﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

public partial class Inventory_Target_Product_List : PageBase
{

    private const string REQUEST_LIST_ID = "id";
    private int listID = 0;
    private const string RESPONSE_LIST_MAINTAIN_PAGE = "Inventory_Target_List.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[REQUEST_LIST_ID] != null)
        {
            int.TryParse(Request.QueryString[REQUEST_LIST_ID], out listID);
        }
        if (!IsPostBack)
        {
            GridPager1.GridView = gvChannelType;
            GridPager1.CurrentPage = PageIndex;
            this.InitForm();
            ddlSelected.SelectedValue = "1";
            BindGrid();
            ddlSelected.SelectedValue = "0";
        }
    }

    private void InitForm()
    {
        ICO_PROFILES list = new IcoProfileManager().Load(listID);
        if (list != null)
        {
            lblListName.Text = list.CODE;
        }

        #region 绑定制造商

        ddlManu.DataSource = ManufacturersManager.GetManus(MyStateManager.DomainID);
        ddlManu.DataTextField = "ManuName";
        ddlManu.DataValueField = "ManuCode";
        ddlManu.DataBind();
        ddlManu.Items.Insert(0, new ListItem("--所有--", ""));

        #endregion

        #region 绑定品类

        ddlCategroy.DataSource = CategoryManager.GetCategories(MyStateManager.DomainID);
        ddlCategroy.DataTextField = "CategoryName";
        ddlCategroy.DataValueField = "CategoryCode";
        ddlCategroy.DataBind();
        ddlCategroy.Items.Insert(0, new ListItem("--所有--", ""));

        #endregion

        #region 绑定品牌

        ddlBrand.DataSource = BrandManager.GetBrands(MyStateManager.DomainID);
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        ddlBrand.DataBind();
        ddlBrand.Items.Insert(0, new ListItem("--所有--", ""));

        #endregion
    }

    private void BindGrid()
    {
        if (listID > 0)
        {
            //DataSet ds = UserProductListItemManager.GetIcoProfileItem(listID,
            //                                                                        ddlCategroy.SelectedValue,
            //                                                                        ddlBrand.SelectedValue,
            //                                                                        ddlManu.SelectedValue,
            //                                                                        this.MyStateManager.DomainID,
            //                                                                        ddlSelected.SelectedValue);
            //GridPager1.DataSource = ds.Tables[0];
            GridPager1.BindData();
        }
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (listID > 0)
        {
            for (int i = 0; i < gvChannelType.Rows.Count; i++)
            {
                decimal dbPiecePrice;
                int itemID = 0;

                int productID = Convert.ToInt32(gvChannelType.DataKeys[i].Value);

                string PIECE_PRICE = ((TextBox)gvChannelType.Rows[i].FindControl("txtP_SKU")).Text;
                decimal.TryParse(PIECE_PRICE, out dbPiecePrice);

                bool IsSelected = ((CheckBox)gvChannelType.Rows[i].FindControl("Selected")).Checked;

                Control ctlItemID = gvChannelType.Rows[i].FindControl("lblItemID");
                if (ctlItemID != null)
                {
                    Label lblItemID = ctlItemID as Label;
                    if (lblItemID != null)
                    {
                        int.TryParse(lblItemID.Text, out itemID);
                    }
                }
                if (IsSelected || (!IsSelected && itemID > 0))
                {
                    CustomerProductListManager.AssignICOProfileProductListItem(IsSelected,
                                                                    itemID,
                                                                    this.listID,
                                                                    productID,
                                                                    dbPiecePrice,
                                                                    this.MyStateManager.Name,
                                                                    MyStateManager.DomainID);
                }
            }
        }
        Response.Redirect(RESPONSE_LIST_MAINTAIN_PAGE);
    }

    protected void ddlCategroy_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        ddlBrand.DataSource = BrandManager.GetBrandsListDaynamic(ddlCategroy.SelectedValue,ddlManu.SelectedValue ,MyStateManager.DomainID);
        ddlBrand.DataBind();
        ddlBrand.Items.Insert(0, new ListItem("--所有--", ""));
    }

    //protected void gvChannelType_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    Control ctl = e.Row.FindControl("Selected");
    //    CheckBox ckb = ctl as CheckBox;
    //    if (ckb != null)
    //    {
    //        DataRowView drv=e.Row.DataItem as DataRowView;
    //        if(drv!=null)
    //        {
    //            ckb.Checked = bool.Parse( drv["Selected"].ToString());
    //        }
    //    }
    //}
    protected void ddlManu_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        ddlBrand.DataSource = BrandManager.GetBrandsListDaynamic(ddlCategroy.SelectedValue, ddlManu.SelectedValue, MyStateManager.DomainID);
        ddlBrand.DataBind();
        ddlBrand.Items.Insert(0, new ListItem("--所有--", ""));
    }
}

